//
//  MYGCloudDiskCommonShareFileHeaderView.swift
//  MYDoctor
//
//  Created by Denny on 2024/3/14.
//

import UIKit

class MYGCloudDiskCommonShareFileHeaderView: UIView {
    
    var headerAvatarView: UIView = {
        let control = UIView()
        return control
    }()
    var buttons = [UIButton]()
    var tapIndexCompeltion:((Int)->Void)?
    var tapDownloadCompeltion:(()->Void)?
    var tapManagrCompeltion:(()->Void)?
    lazy var inviteBtn: UIButton = {
        let control = UIButton()
        control.setTitle("+邀请", for: .normal)
        control.backgroundColor = UIColor.init(hexString: "#F1F9FC")
        control.addCorner(radius: 12.5)
        control.titleLabel?.font = UIFont.medium(13)
        control.setTitleColor(UIColor.init(hexString: "#4583FA"), for: .normal)
        control.addTarget(self, action: #selector(tapInvite), for: .touchUpInside)
        return control
    }()
    
    lazy var managerBtn: UIButton = {
        let control = UIButton()
        control.setTitle("管理", for: .normal)
        control.backgroundColor = UIColor.init(hexString: "#F1F9FC")
        control.addCorner(radius: 12.5)
        control.titleLabel?.font = UIFont.medium(13)
        control.setTitleColor(UIColor.init(hexString: "#4583FA"), for: .normal)
        control.addTarget(self, action: #selector(tapManager), for: .touchUpInside)
        return control
    }()
    
    var redLB:UILabel = {
        let control = UILabel()
        control.backgroundColor = UIColor.init(hexString: "#FF0000")
        control.textColor = .white
        control.font = .medium(7)
        control.isHidden = true
        control.textAlignment = .center
        control.layer.cornerRadius = 6
        control.layer.masksToBounds = true
        return control
    }()
    
    //下载按钮
    lazy var downloadBtn: UIButton = {
        let control = UIButton()
        control.setImage(UIImage.init(named: "MYG_clouldDisk_download"), for: [])
        control.addTarget(self, action: #selector(tapDownloadAction), for: .touchUpInside)
        return control
    }()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        NotificationCenter.default.addObserver(self, selector: #selector(reloadRedLB), name: Notification.Name("MYCloudDiskHeaderActionView_reloadRedLB"), object: nil)

        addSubview(headerAvatarView)
        headerAvatarView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(25)
            make.top.equalToSuperview().offset(15)
            make.width.equalTo(100)
            make.height.equalTo(35)
        }
        
        addSubview(inviteBtn)
        inviteBtn.snp.makeConstraints { make in
            make.size.equalTo(CGSize.init(width: 56, height: 25))
            make.centerY.equalTo(headerAvatarView.snp.centerY)
            make.left.equalTo(headerAvatarView.snp.right)
        }
        addSubview(managerBtn)
        managerBtn.snp.makeConstraints { make in
            make.size.equalTo(CGSize.init(width: 56, height: 25))
            make.centerY.equalTo(inviteBtn.snp.centerY)
            make.left.equalTo(inviteBtn.snp.right).offset(12)
        }
          
        let titles = ["全部","图片","视频","文件"]
        let buttonWidth = 58
        let marginLeft = 15
        let midMrgin = 23
        for (i,title) in titles.enumerated() {
            let button = UIButton.init(frame: CGRect.init(x: marginLeft + (buttonWidth + midMrgin) * i, y: 70, width: buttonWidth, height: 21))
            button.setTitle(title, for: .normal)
            button.titleLabel?.font = UIFont.systemFont(ofSize: 12)
            button.addTarget(self, action: #selector(tapButton(_ :)), for: .touchUpInside)
            button.tag = i
            button.addCorner(radius: 3)
            addSubview(button)
            buttons.append(button)
        }
        if let first = buttons.first {
            tapButton(first)
        }
        
        addSubview(downloadBtn)
        downloadBtn.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-20)
            make.size.equalTo(CGSize.init(width: 35, height: 35))
            make.top.equalTo(inviteBtn.snp.bottom).offset(18)
        }
        addSubview(redLB)
        redLB.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-20)
            make.size.equalTo(CGSize.init(width: 12, height: 12))
            make.top.equalTo(inviteBtn.snp.bottom).offset(18)
        }
        reloadRedLB()
        reloadAvatarView()
    }
    
    func reloadAvatarView() {
        guard let user = MYGUserManager.getUser() else {return}
        let users = [user.doctor]
        let wwidth = 35
        for(i,obj) in users.enumerated() {
            let header = UIImageView()
            header.sd_setImage(with: URL(string: obj.avatar), placeholderImage: UIImage.init(named: "head"), context: nil)
            header.layer.cornerRadius = 17.5
            header.layer.masksToBounds = true
            header.frame = CGRect(x: (wwidth - 10) * i, y: 0, width: wwidth, height: wwidth)
            headerAvatarView.addSubview(header)
        }
    }
    
    
    
    @objc func reloadRedLB(){
        let count = MYGUploadManager.shared.uploadingArray.count + MYGDownloadManager.shared.downlodingArray.count
        self.redLB.isHidden = count == 0
        self.redLB.text = "\(count)"
    }
    
    func tapFirstBtn() {
        if let first = buttons.first {
            tapButton(first)
        }

    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    @objc func tapInvite() {
        MYGCloudDiskPopManager.showInviteSheet(self.rootResponsder().view)
        
    }
    @objc func tapManager() {
        if let c = tapManagrCompeltion {
            c()
        }
    }
    
    @objc func tapButton(_ sender:UIButton) {
        for button in buttons {
            if button.tag == sender.tag {
                button.backgroundColor = UIColor.init(hexString: "#557EED")
                button.setTitleColor(.white, for: .normal)
            }else {
                button.backgroundColor = UIColor.init(hexString: "#F5F5F5")
                button.setTitleColor(UIColor.init(hexString: "#565656"), for: .normal)
            }
        }
        if let tap = tapIndexCompeltion {
            tap(sender.tag)
        }
    }
    @objc func tapDownloadAction() {
        if let tap = tapDownloadCompeltion {
            tap()
        }
    }
}
